<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Rates extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}
	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	 }

	public function search_rate(){
		$name = $this->input->post('search_rate_name');
		$desc = $this->input->post('search_rate_desc');
		$query = $this->rates_model->search_rates($name,$desc);

		if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}
	}
	public function insert_rate(){
	 	$ena = $this->input->post('ena_rate');
	 	$data = array(
	 		':p_bu_id'			=> $this->session->userdata('bu_id'),
	 		':p_rate_name'		=> $this->input->post('rate_name'),
	 		':p_description'	=> $this->input->post('rate_description'),
	 		':p_enable_flag'	=> ($ena == "ok" ? 'Y' : 'N'),
	 		':p_user_id'		=> $this->session->userdata('user_id'),
	 		':p_chargelist_id'  => $this->input->post('chargelist_id')
	 		);
	 	$this->rates_model->insert_rates($data);
	 }
	 public function update_rate(){
	 	$ena = $this->input->post('edit_ena_rate');
	 	$data = array(		
	 		':p_rate_id'		=> $this->input->post('hidden_rate_id'),
	 		':p_rate_name'		=> $this->input->post('edit_rate_name'),
	 		':p_description'	=> $this->input->post('edit_rate_description'),
	 		':p_enable_flag'	=> ($ena == "ok" ? 'Y' : 'N'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$this->rates_model->update_rates($data);
	 }




	public function insert_rate_sched(){
	 	$data = array(
	 		':p_rate_id'		=> $this->input->post('hidden_rate_id'),
	 		':p_charge_code'	=> $this->input->post('r_chg_name'),
	 		':p_description'	=> $this->input->post('r_desc'),
	 		':p_usage_qty'		=> $this->input->post('r_usage'),
	 		':p_rate'			=> $this->input->post('r_rate'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$query= $this->rates_model->insert_rates_sched($data);
	 	if($query[0]->error == 1){
			echo json_encode($query);
		}else{
			$this->view_sched_list($this->input->post('hidden_rate_id'));
		}
	 }
	 public function update_rate_sched(){
	 	$data = array(
	 		':p_rate_id'		=> $this->input->post('edit_hidden_rate_id'),
	 		':p_charge_code'	=> $this->input->post('edit_r_chg_name'),
	 		':p_description'	=> $this->input->post('edit_r_desc'),
	 		':p_usage_qty'		=> $this->input->post('edit_r_usage'),
	 		':p_rate'			=> $this->input->post('edit_r_rate'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$query= $this->rates_model->insert_rates_sched($data);
	 	if($query[0]->error == 1){
			echo json_encode($query);
		}else{
			$this->view_sched_list($this->input->post('edit_hidden_rate_id'));
		}
	 }

	public function update_inline_sched(){
		$bm = $this->input->post('min_flag');
		$dm = $this->input->post('mindf_flag');
		$final_min = '';
		 if($bm == "yes"){ $final_min = 'Y'; }
		 else if($bm == "no" && $dm == "yes"){ $final_min = 'X'; }
		$data = array(
	 		':p_line_id'		=> $this->input->post('line-id'),
	 		':p_usage_qty'		=> $this->input->post('usage'),
	 		':p_rate'			=> $this->input->post('rate'),
	 		':p_min_flag'		=> $final_min,
	 		':p_rate_id'		=> $this->input->post('rates_id'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);
		$this->rates_model->update_inline_sched($data);
	}
	public function view_sched_list($id){
		$query = $this->rates_model->get_sched($id);
		echo json_encode($query);
	}



	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
			'elements'		=> $this->rates_model->get_charges(),
			'chargelist'	=> $this->rates_model->getChargeList()
		));
	}
	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
			'elements'		=> $this->rates_model->get_charges(),
			'rates_data'	=> $this->rates_model->get_rates_data($id)
		));
	}
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
			'sched_data'	=> $this->rates_model->get_sched($id),
			'rates_data'	=> $this->rates_model->get_rates_data($id)
		));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */